package com.efast.dmom.mes.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.efast.dmom.mes.bean.OperationWorkorder;
import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.bean.RouteStep;
import com.efast.dmom.mes.dao.entity.OperationWorkorderDao;
import com.efast.dmom.mes.dao.entity.PlanDao;
import com.efast.dmom.mes.dao.entity.ProductRouteDao;
import com.efast.dmom.mes.dao.entity.SubmittedWorkRetentionDao;
import com.efast.dmom.mes.entity.OperationWorkorderEntity;
import com.efast.dmom.mes.entity.PlanEntity;
import com.efast.dmom.mes.entity.ProductRouteEntity;
import com.efast.dmom.mes.entity.SubmittedWorkRetentionEntity;
import com.efast.dmom.mes.service.OperationWorkorderService;
import com.efast.dmom.mes.util.CommonConstants;
import com.efast.dmom.mes.util.PlanOrderCommonConstants;
import com.efast.dmom.mes.util.ServiceBaseEntityUtil;
import com.efast.dmom.mes.util.SpringWebUtils;
import com.efast.dmom.mes.util.TimeUtil;

import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example.Criteria;
@Service
@Transactional
public class OperationWorkorderServiceImpl implements OperationWorkorderService {

	@Autowired
	private OperationWorkorderDao operationWorkorderDao;
	
	@Autowired
	private PlanDao planDao;
	
	@Autowired
	private ProductRouteDao productRouteDao;
	
	@Autowired
	private ProductionImplementationServiceImpl productionImplementationServiceImpl;
	
	@Autowired
	private ServiceBaseEntityUtil serviceBaseEntityUtil;
	
	@Autowired
	private SubmittedWorkRetentionDao submittedWorkRetentionDao;

	@Override
	public ResultBean batchAddData(OperationWorkorder operationWorkorder) {
		ResultBean resultBean = new ResultBean(true);
		// 通过计划id获取计划信息
		PlanEntity byId = planDao.getById(operationWorkorder.getPlanId());
		if (byId == null) {
			resultBean.setSuccess(false);
			resultBean.setErrorMessage("传输的计划id有误，无法查询出当前计划！");
			return resultBean;
		}
		// 判断是否已下发数据
		Condition condition = new Condition(OperationWorkorderEntity.class);
		Criteria createCriteria = condition.createCriteria();
		createCriteria.andEqualTo("planNo", byId.getPlanNo());
		List<OperationWorkorderEntity> selectByExample = operationWorkorderDao.selectByExample(condition);
		if (selectByExample.size() >0) {
			resultBean.setSuccess(false);
			resultBean.setErrorMessage("当前计划已下发，不可重复下发！");
			return resultBean;
		}
		// 通过物料id找到物料工艺配置关系
		List<ProductRouteEntity> byPartId = productRouteDao.getByPartId(byId.getPartId());
		if (byPartId.size() == 0) {
			resultBean.setSuccess(false);
			resultBean.setErrorCode("通过物料id未查询出产品工艺配置信息");
			return resultBean;
		} else if (byPartId.size() > 1) {
			resultBean.setSuccess(false);
			resultBean.setErrorCode("通过物料id查询出多条产品工艺配置信息！");
			return resultBean;
		}
		// 通过工艺id获取下面所有的工序
		List<RouteStep> allRouteStepByRouteKey = productionImplementationServiceImpl.getAllRouteStepByRouteKey(byPartId.get(0).getRouteId());
		// 遍历插入数据
		long i = 1;
		for (RouteStep routeStep : allRouteStepByRouteKey) {
			OperationWorkorder operationWorkorder2 = new OperationWorkorder();
			operationWorkorder2.setCreateTime(TimeUtil.getLocalDateTime());
			operationWorkorder2.setCreateUser(SpringWebUtils.getRequestUser());
			operationWorkorder2.setOperationId(routeStep.getInnerData().getOperationKey());
			operationWorkorder2.setSequence(i++);
			operationWorkorder2.setOperationType(routeStep.getInnerData().getOperationType());
			String no = serviceBaseEntityUtil.getNo("PLAN_ORDER");
			operationWorkorder2.setOrderNumber(no);
			operationWorkorder2.setOrderStatus(PlanOrderCommonConstants.PlanOrderRelease);
			operationWorkorder2.setPartId(byId.getPartId());
			operationWorkorder2.setPlanId(byId.getId());
			operationWorkorder2.setPlanNo(byId.getPlanNo());
			operationWorkorder2.setQuatity((long) byId.getQuantity());
			operationWorkorder2.setIsFlag(0L);
			operationWorkorderDao.insertSelective(operationWorkorder2);
		}
		byId.setLastModifiedTime(TimeUtil.getLocalDateTime());
		byId.setLastModifier(SpringWebUtils.getRequestUser());
		byId.setPlanStatus(CommonConstants.JRD_PLAN_STATUS_RELEASE);
		planDao.updatePlanStatus(byId);
		return resultBean;
	}

	@Override
	public ResultBean updateQuatity(OperationWorkorder operationWorkorder) {
		ResultBean resultBean = new ResultBean(true);
		
		
		OperationWorkorder operationWorkorder2 = new OperationWorkorder();
		operationWorkorder2.setId(operationWorkorder.getId());
		operationWorkorder2.setQuatity(operationWorkorder.getQuatity());
		operationWorkorder2.setIsFlag(1L);
		operationWorkorder2.setLastUpdateTime(TimeUtil.getLocalDateTime());
		operationWorkorder2.setLastUpdateUser(SpringWebUtils.getRequestUser());
		// 更新数量字段
		operationWorkorderDao.updateByPrimaryKeySelective(operationWorkorder2);
		return resultBean;
	}
	

}
